part of 'page.dart';

class _SingleCustomCollectionFolderPageWlocStateFactory {
  const _SingleCustomCollectionFolderPageWlocStateFactory._internal();

  static const _instance = _SingleCustomCollectionFolderPageWlocStateFactory._internal();

  factory _SingleCustomCollectionFolderPageWlocStateFactory() {
    return _instance;
  }

  _SingleCustomCollectionFolderPageWlocState create({
    required bloc.SingleCustomCollectionFolderPageBlocState blocState,
  }) {
    switch (blocState) {
      case bloc.DefaultState():
        final collectionModels = blocState.collectionFolder.collections.map((collection) {
          final permitted = blocState.permittedCollectionListItemIds.contains(collection.id);

          return _DefaultStateCollectionModel(
            id: collection.id,
            title: collection.title,
            permitted: permitted,
          );
        }).toBuiltList();

        final collectionFolder = blocState.collectionFolder;

        final collectionFolderModel = _DefaultStateCollectionFolderModel(
          title: collectionFolder.title,
          collectionModels: collectionModels,
        );

        final collectionDeletionActionTileShown = collectionModels.isNotEmpty;
        final collectionReorderingActionTileShown = collectionModels.length > 1;

        return _DefaultState(
          collectionFolderModel: collectionFolderModel,
          collectionCreationPermitted: blocState.collectionCreationPermitted,
          collectionDeletionActionTileShown: collectionDeletionActionTileShown,
          collectionReorderingActionTileShown: collectionReorderingActionTileShown,
        );

      case bloc.CollectionReordering():
        final reorderedCollectionModels = blocState.reorderedCollections.map((collection) {
          final permitted = blocState.permittedCollectionListItemIds.contains(collection.id);

          return _CollectionReorderingCollectionModel(
            id: collection.id,
            title: collection.title,
            permitted: permitted,
          );
        }).toBuiltList();

        return _CollectionReordering(
          reorderedCollectionModels: reorderedCollectionModels,
          collectionFolderTitle: blocState.collectionFolder.title,
        );

      case bloc.CollectionCreation():
        final collectionModels = blocState.collectionFolder.collections.map((collection) {
          final permitted = blocState.permittedCollectionListItemIds.contains(collection.id);

          return _CollectionCreationCollectionModel(
            id: collection.id,
            title: collection.title,
            permitted: permitted,
          );
        }).toBuiltList();

        final collectionDeletionActionTileShown = collectionModels.isNotEmpty;
        final collectionReorderingActionTileShown = collectionModels.length > 1;

        return _CollectionCreation(
          collectionModels: collectionModels,
          collectionCreationPermitted: blocState.collectionCreationPermitted,
          collectionDeletionActionTileShown: collectionDeletionActionTileShown,
          collectionReorderingActionTileShown: collectionReorderingActionTileShown,
        );

      case bloc.CollectionDeletion():
        final collectionModels = blocState.collectionFolder.collections.map((collection) {
          final permitted = blocState.permittedCollectionListItemIds.contains(collection.id);
          final selected = blocState.selectedCollectionIds.contains(collection.id);

          return _CollectionDeletionCollectionModel(
            id: collection.id,
            title: collection.title,
            permitted: permitted,
            selected: selected,
          );
        }).toBuiltList();

        final allCollectionSelected =
            collectionModels.length == blocState.selectedCollectionIds.length;

        return _CollectionDeletion(
          collectionModels: collectionModels,
          allCollectionSelected: allCollectionSelected,
        );

      case bloc.CollectionFolderRenaming():
        final collectionModels = blocState.collectionFolder.collections.map((collection) {
          final permitted = blocState.permittedCollectionListItemIds.contains(collection.id);

          return _CollectionFolderRenamingCollectionModel(
            id: collection.id,
            title: collection.title,
            permitted: permitted,
          );
        }).toBuiltList();

        final collectionFolder = blocState.collectionFolder;

        final collectionFolderModel = _CollectionFolderRenamingCollectionFolderModel(
          title: collectionFolder.title,
          collectionModels: collectionModels,
        );

        final collectionDeletionActionTileShown = collectionModels.isNotEmpty;
        final collectionReorderingActionTileShown = collectionModels.length > 1;

        return _CollectionFolderRenaming(
          collectionFolderModel: collectionFolderModel,
          collectionCreationPermitted: blocState.collectionCreationPermitted,
          collectionDeletionActionTileShown: collectionDeletionActionTileShown,
          collectionReorderingActionTileShown: collectionReorderingActionTileShown,
        );
    }
  }
}
